package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RequestMapping("/admin/dish")
@RestController
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("添加菜品")
    @PostMapping
    public Result add(@RequestBody DishDTO dishDTO) {
        log.info("添加菜品");
        // 调用service层
        dishService.add(dishDTO);
        // 清理缓存
        Long categoryId = dishDTO.getCategoryId();
        clean("dish_"+categoryId);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页")
    public Result page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页");
        // 调用service层,需要返回一个pageResult对象
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteById(@RequestParam List<Long> ids) {
        log.info("批量删除菜品");
        // 调用service层
        dishService.deleteById(ids);
        clean("dish_*");
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return 返回一个 DishVO
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result findById(@PathVariable Long id) {
        DishVO dishVO = dishService.findById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        Long categoryId = dishDTO.getCategoryId();
        clean("dish_"+categoryId);
        return Result.success();
    }

    /**
     * 修改菜品启用状态
     *
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品启用状态")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        // 调用service层
        dishService.updateStatus(status, id);
        clean("dish_*");
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result findByCategoryId(Long categoryId){
        List<Dish> dishList = dishService.findByCategoryId(categoryId);
        return Result.success(dishList);
    }

    private void clean(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
